package com.survey.Controller;

import com.survey.pojo.data.LinkDO;
import com.survey.pojo.vo.LinkVO;
import com.survey.pojo.vo.Query;
import com.survey.service.LinkService;
import com.survey.utils.DataUtils;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/link")
public class LinkController {

    @Resource
    LinkService linkService;

    /**
     * link/AddLink.do?op=AddLink
     * url: hhh
     * sitename: 222
     * description: 2222
     */
    @RequestMapping("/addLink")
    @ResponseBody
    public Result addLink(String op, HttpServletRequest request) throws Exception {
        String url = request.getParameter("url");
        String sitename=request.getParameter("sitename");
        LinkVO linkVO=new LinkVO();
        linkVO.setUrl(url);
        List<LinkDO> linkDOS = linkService.listLink(linkVO);
        if(linkDOS.isEmpty()){
            String description=request.getParameter("description");
            linkVO.setName(sitename);
            linkVO.setInfo(description);
            Date date=new Date();
            linkVO.setAddtime(date);
            Result result = linkService.addLink(linkVO);
            return result;
        }
        return new Result(Result.CODE_FAILURE);

    }

    @RequestMapping("/manage")
    public String manage(LinkVO linkVO, Model model) throws InvocationTargetException, IllegalAccessException {
       return manageByPage(1,linkVO,model);
    }

    @RequestMapping("/manage/{pageCode}")
    public String manageByPage(@PathVariable Integer pageCode, LinkVO linkVO, Model model) throws InvocationTargetException, IllegalAccessException {
        linkVO.setPageSize(5);
        linkVO.setSize(5);
        linkVO.setPageCode(DataUtils.getPageCode(pageCode+""));
        PageBean<LinkVO> pageBean =linkService.listLinkByPage(linkVO);
        model.addAttribute("pageBean",pageBean);
        return "/admin/FriendLinkAdmin.jsp";
    }

    @RequestMapping("/getLink")
    public String getLink(Integer lid,Model model) throws Exception {
        LinkVO linkVO=new LinkVO();
        linkVO.setId(lid);
        LinkDO detailLink = linkService.findDetailLink(linkVO);
        model.addAttribute("detailLink",detailLink);
        return "/admin/LinkModifg.jsp";
    }

    /**
     * lid: 13
     * urlLink: https://github.com/
     * siteName: 开源网站
     * description: github
     * submit: 完成
     * id url name img info addTime
     */
    @RequestMapping("/editLink")
    @ResponseBody
    public Result editLink(Integer lid,String urlLink,String siteName,String description,HttpServletRequest request) throws Exception {
       LinkVO linkVO=new LinkVO();
       linkVO.setId(lid);
       linkVO.setUrl(urlLink);
       linkVO.setName(siteName);
       linkVO.setInfo(description);
        return linkService.updateLink(linkVO);
    }

    @RequestMapping("/delLink")
    @ResponseBody
    public Result delLink(Integer lid) throws Exception {
        LinkVO linkVO=new LinkVO();
        linkVO.setId(lid);
        return linkService.deleteLink(linkVO);
    }

}


/**
 数据：

 27	https://www.51job.com	前程无忧	找工作
 28	http://www.chinahr.com	中华英才网
 29	https://www.mgtv.com/	芒果tv	青少年极其喜欢
 */